<?php
/**
 * @title 1005. K 次取反后最大化的数组和 - 最大堆
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer
     */
    function largestSumAfterKNegations($nums, $k) {
        /**
         * @since 2021-12-03 负数组
         */
        $nums1 = [];

        /**
         * @since 2021-12-03 数组之和
         * @since 2021-12-03 sum1, 正数之和
         * @since 2021-12-03 sum2, 负数变正数之和
         * @since 2021-12-03 sum3, 依然是负数之和
         */
        $sum1 = $sum2 = $sum3 = 0;

        /**
         * @since 2021-12-03 正数组最小值, 题目限制最大值100
         * @since 2021-12-03 负数组最大值，题目限制最小值-100
         */
        $min = 1000;
        $max = -1000;

        /**
         * @since 2021-12-03 最大堆
         */
        $iHeap = new SplMaxHeap();

        /**
         * @since 2021-12-03 分组
         */
        foreach ($nums as $num){
            /**
             * @since 2021-12-03 正数，最小值，求和
             */
            if($num >= 0){
                $min = min($min, $num);
                $sum1 += $num;
            } else {
                /**
                 * @since 2021-12-03 负数最大数
                 * @since 2021-12-03 负数, 最小堆
                 */
                $max = max($max, $num);

                if($iHeap->count() < $k){
                    $iHeap->insert($num);
                    $sum2 -= $num;
                } else {
                    if($num < $iHeap->top()){
                        /**
                         * @since 2021-12-03 弹出最大数
                         */
                        $top = $iHeap->extract();
                        $sum3 += $top;

                        /**
                         * @since 2021-12-03 更小数入堆
                         */
                        $sum2 = $sum2 + $top - $num;
                        $iHeap->insert($num);
                    } else {
                        $sum3 += $num;
                    }
                }
            }
        }

        /**
         * @since 2021-12-03 判断k是否有剩余，剩余是偶数
         * @since 2021-12-03 或者有结余，存在元素0
         */
        $sum = $sum1 + $sum2 + $sum3;
        if(($k-$iHeap->count())%2==0){
        } else {
            /**
             * @since 2021-12-03 正数最小数，负数最大数
             */
            $max = abs($max);
            if($max != 1000 AND $min != 1000){
                $sum = $sum - 2*min($max, $min);
            } elseif($max == 1000) {
                $sum = $sum - 2*$min;
            } else {
                $sum = $sum - 2*$max;
            }
        }

        /**
         * @return
         */
        return $sum;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202112/2021.12.03.php
 */
$datas = [
    [[4,2,3], 1], // 5
    [[3,-1,0,2], 3], // 6
    [[2,-3,-1,5,-4], 2], // 13
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->largestSumAfterKNegations(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
